//
//  VKCollectionViewCell.m
//  GolfFriend
//
//  Created by Vescky on 14-2-12.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "VKCollectionViewCell.h"
#import <ImageIO/ImageIO.h>
#import <QuartzCore/QuartzCore.h>
#import "DateUtility.h"
#import "AppDelegate.h"
#import "SVProgressHUD.h"
@interface VKCollectionViewCell () {
    
}

@end

@implementation VKCollectionViewCell

@synthesize contentArr,delegate,selectedPhotos;

- (void)setDataInfo:(NSMutableArray*)_arr {
    if (!_arr || [_arr count] < 1) {
        return;
    }
    contentArr = _arr;
    selectedPhotos = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < [contentArr count]; i++) {
        
        NSDictionary *_dict = [contentArr objectAtIndex:i];
        UIView *unitView = [self getUnitViewWithContent:_dict order:i];
        [self.contentView addSubview:unitView];
    }
    
}

- (UIView*)getUnitViewWithContent:(NSDictionary*)_dict order:(int)_index {
    
    float margin = 5;
    if (_index >= 1) {
        margin = 6;//中间的间隔大1px
    }
    CGRect uRect = CGRectMake(5, 2, 96, 96);
    if (_index >= 1) {
        uRect.origin.x = uRect.origin.x + uRect.size.width + margin;
    }
    if (_index >= 2) {
        uRect.origin.x = uRect.origin.x + uRect.size.width + margin;
    }
    
    //生成单元view
    UIView *unitView = [[UIView alloc] initWithFrame:uRect];
    [unitView setBackgroundColor:GetColorWithRGB(240, 240, 240)];
    unitView.tag = 100 + _index * 10;//区分单元
    if ([[_dict objectForKey:@"cellUnitType"] integerValue] == CameraStyle) {
        //相机按钮模式
        UIImageView *imgv = [[UIImageView alloc] initWithFrame:CGRectMake(19, 24, 58, 48)];
        [imgv setImage:[UIImage imageNamed:@"comment_29.png"]];
        [unitView addSubview:imgv];
    }
    else {
        //照片模式
        UIImage *_img = [_dict objectForKey:@"thumbnail"];
        NSLog(@"width:%f,height:%f",_img.size.width,_img.size.height);
        if (!_img) {
            return nil;
        }
        UIImageView *contentImgv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, uRect.size.width, uRect.size.height)];
        [contentImgv setImage:_img];
        [unitView addSubview:contentImgv];
        
        //checkbox
        UIView *checkView = [[UIView alloc] initWithFrame:CGRectMake(unitView.frame.size.width - 22 - 2, 2, 22, 22)];
        [checkView setBackgroundColor:[UIColor blackColor]];
        [checkView setAlpha:0.8];//半透明
        
        if (![_dict objectForKey:@"isSelected"]) {
            [checkView setHidden:YES];
        }
        
        checkView.layer.cornerRadius = checkView.frame.size.width / 2;
        UIImageView *checkImgv = [[UIImageView alloc] initWithFrame:CGRectMake(2, 2, checkView.frame.size.width - 4, checkView.frame.size.height - 4)];
        [checkImgv setImage:[UIImage imageNamed:@"check_green.png"]];
        [checkView addSubview:checkImgv];
        [unitView addSubview:checkView];
        
        NSString *dateString = getStringFromDate(@"yyyy-MM-dd", [_dict objectForKey:@"date"]);
        UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, uRect.size.height - 20, uRect.size.width, 20)];
        [bView setBackgroundColor:[UIColor darkGrayColor]];
        [bView setAlpha:0.7];
        [bView setClipsToBounds:YES];
        
        UILabel *labelDate = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, bView.frame.size.width, bView.frame.size.height - 2)];
        [labelDate setFont:[UIFont boldSystemFontOfSize:15.0]];
        [labelDate setTextColor:[UIColor whiteColor]];
        [labelDate setTextAlignment:NSTextAlignmentCenter];
        [labelDate setText:dateString];
        [labelDate setBackgroundColor:[UIColor clearColor]];
        
        [bView addSubview:labelDate];
        [unitView addSubview:bView];
        
        contentImgv.tag = unitView.tag + 1;
        checkView.tag = unitView.tag + 2;
        labelDate.tag = unitView.tag + 3;
    }
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, uRect.size.width, uRect.size.height)];
    btn.tag = unitView.tag;//这样就知道选中哪个view了
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn addTarget:self action:@selector(btnDidPressed:) forControlEvents:UIControlEventTouchUpInside];
    [unitView addSubview:btn];
    
    return unitView;
}


- (NSDate*)getimageDate:(NSString*)imgPath {
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:imgPath withExtension:nil];
    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[modelURL path] error:nil];
    NSLog(@"%@",fileAttributes);
    return [fileAttributes objectForKey:@"NSFileCreationDate"];
}

- (bool)isUnitSelected:(NSString*)imgPath {
    for (int i = 0; i < [selectedPhotos count]; i++) {
        if ([[selectedPhotos objectAtIndex:i] isEqualToString:imgPath]) {
            return YES;
        }
    }
    return NO;
}

- (IBAction)btnDidPressed:(id)sender {
    
    if ([[APP_DELEGATE photoAlbumSelectedArray] count] >= Max_Article_Photo) {
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"最多可选%d张照片",Max_Article_Photo]];
        return;
    }
    
    UIButton *btn = (UIButton*)sender;
    NSLog(@"btn pressed!No.%d",btn.tag);
    int sIndex = (btn.tag - 100) / 10;
    if (sIndex < 0 || sIndex  >= [contentArr count]) {
        return;
    }
    NSDictionary *_dict = [contentArr objectAtIndex:sIndex];
    
    if ([[_dict objectForKey:@"cellUnitType"] integerValue] == PhotoStyle) {
        //照片模式，可选
        UIView *sUnitView = [self viewWithTag:btn.tag];
        UIView *checkView = [sUnitView viewWithTag:btn.tag + 2];
        [checkView setHidden:!checkView.hidden];
        
        if (checkView.hidden) {
            if ([_dict objectForKey:@"imagePath"]) {
                [selectedPhotos removeObject:[_dict objectForKey:@"imagePath"]];
                [[APP_DELEGATE photoAlbumSelectedArray] removeObject:[_dict objectForKey:@"imagePath"]];
            }
        }
        else {
            if ([_dict objectForKey:@"imagePath"]) {
                [selectedPhotos addObject:[_dict objectForKey:@"imagePath"]];
                [[APP_DELEGATE photoAlbumSelectedArray] addObject:[_dict objectForKey:@"imagePath"]];
            }
        }
        
        NSLog(@"current select photos:%@",selectedPhotos);
    }
    
    if ([self.delegate respondsToSelector:@selector(VKCollectionViewCellDidPressed:)]) {
        [self.delegate VKCollectionViewCellDidPressed:_dict];
    }
    
}

@end
